var common = require('../../common.js');
var app = getApp();
var videoContext;
var videolist= [];
var index= 0;
var countNum= 0;
Page({
  data: {
    appname:"",
    video_id: 0,
    detail: {},
    user: {},
    init: {},
    xheight: 1000,
    endshow: false,
    white: false,
    items: [],
    page: 1,
    adstart: '',
    showadd: false
  },
  //上滑加载更多函数   使用竖向滚动时，需要给<scroll-view/>一个固定高度，通过 WXSS 设置 height。
  onReachBottom: function (e) {
    //上滑动加载 变化的是加载数据的序号初始1-20  再次上滑加载20-40 star= &count=    由于后台接口没有那么多数据看不到效果
    //设定要加载的url
    this.setData({
      endshow: false
    });
    if (this.countNum < this.index) {
      this.countNum++;
      console.info(this.countNum)
        }
              // 播放增次
              app.globalData.playCount++;
              // 数据处理
    this.data.detail = this.videolist[this.countNum];

          // 前贴广告
          var appInfo = app.globalData.appInfo;
          var playCount = app.globalData.playCount;
          if (appInfo.ad_start != '' && appInfo.ad_start_time > 0 && playCount % appInfo.ad_start_time == 0) {
            this.data.adstart = appInfo.ad_start;
          }
          // 收藏提示
          if (appInfo.is_online == 1) {
            var showadd = wx.getStorageSync('showAdd');
            if (showadd == false || showadd == undefined || showadd == null) {
              this.data.showadd = true;
            } else {
              this.data.showadd = false;
            }
          }

          this.setData({
            detail: this.data.detail,
            video_id: this.data.video_id,
            user: this.data.user,
            init: this.data.init,
            adstart: this.data.adstart,
            showadd: this.data.showadd
          });

          // 插屏广告
          if (appInfo.ad_modal != '' && appInfo.ad_modal_time > 0 && playCount % appInfo.ad_modal_time == 0) {
            if (wx.createInterstitialAd) {
              var interstitialAd = wx.createInterstitialAd({
                adUnitId: appInfo.ad_modal
              });
              // 在适合的场景显示插屏广告
              if (interstitialAd) {
                interstitialAd.show().catch((err) => { });
              }
            }
          }
          wx.hideNavigationBarLoading() //完成停止加载
          wx.stopPullDownRefresh() //停止下拉刷新
       
  },
  onPullDownRefresh: function () {
          wx.showNavigationBarLoading() //在标题栏中显示加载
    if (this.countNum <=0) {
      this.countNum = this.index-1;
          } else {
      --this.countNum;
          }
    this.setData({
      endshow: false
    });
          // 播放增次
          app.globalData.playCount++;
          // 数据处理
    this.data.detail = this.videolist[this.countNum];

    

          // 前贴广告
          var appInfo = app.globalData.appInfo;
          var playCount = app.globalData.playCount;
          if (appInfo.ad_start != '' && appInfo.ad_start_time > 0 && playCount % appInfo.ad_start_time == 0) {
            this.data.adstart = appInfo.ad_start;
          }
          // 收藏提示
          if (appInfo.is_online == 1) {
            var showadd = wx.getStorageSync('showAdd');
            if (showadd == false || showadd == undefined || showadd == null) {
              this.data.showadd = true;
            } else {
              this.data.showadd = false;
            }
          }

          this.setData({
            detail: this.data.detail,
            video_id: this.data.video_id,
            user: this.data.user,
            init: this.data.init,
            adstart: this.data.adstart,
            showadd: this.data.showadd
          });

          // 插屏广告
          if (appInfo.ad_modal != '' && appInfo.ad_modal_time > 0 && playCount % appInfo.ad_modal_time == 0) {
            if (wx.createInterstitialAd) {
              var interstitialAd = wx.createInterstitialAd({
                adUnitId: appInfo.ad_modal
              });
              // 在适合的场景显示插屏广告
              if (interstitialAd) {
                interstitialAd.show().catch((err) => { });
              }
            }
          }
          wx.hideNavigationBarLoading() //完成停止加载
          wx.stopPullDownRefresh() //停止下拉刷新
        
      
  },
  onLoad: function (option) {
    this.data.video_id = option.video_id;
    this.getDetail();
  },
  getDetail: function(){
    wx.request({
      url: app.globalData.baseUrl + "/blade-video/video/detail",
      method: 'GET',
      data: {
        token: common.token,
        id: this.data.video_id,
        appid: wx.getAccountInfoSync().miniProgram.appId
      },
      success: res => {
        if (res.data.code == 200) {
          // 播放增次
          app.globalData.playCount++;
          this.videolist = res.data.data.videoList;
          this.index = res.data.data.countNum;
          this.countNum=0;
          // 数据处理
          this.data.detail = res.data.data;
          this.data.user = res.data.data.user;
          this.data.init = res.data.data.init;
          this.data.appname = this.data.detail.appname;

          // 前贴广告
          var appInfo = app.globalData.appInfo;
          var playCount = app.globalData.playCount;
          if (appInfo.ad_start != '' && appInfo.ad_start_time > 0 && playCount % appInfo.ad_start_time == 0) {
            this.data.adstart = appInfo.ad_start;
          }
          // 收藏提示
          if (appInfo.is_online == 1) {
            var showadd = wx.getStorageSync('showAdd');
            if (showadd == false || showadd == undefined || showadd == null) {
              this.data.showadd = true;
            } else {
              this.data.showadd = false;
            }
          }

          this.setData({
            detail: this.data.detail,
            video_id: this.data.video_id,
            user: this.data.user,
            init: this.data.init,
            adstart: this.data.adstart,
            showadd: this.data.showadd
          });

          // 插屏广告
          if (appInfo.ad_modal != '' && appInfo.ad_modal_time > 0 && playCount % appInfo.ad_modal_time == 0) {
            if (wx.createInterstitialAd) {
              var interstitialAd = wx.createInterstitialAd({
                adUnitId: appInfo.ad_modal
              });
              // 在适合的场景显示插屏广告
              if (interstitialAd) {
                interstitialAd.show().catch((err) => { });
              }
            }
          }
        }
      }
    });
    //this.dataLoad();
  },
  onReady: function () {
    var system = wx.getSystemInfoSync();
    videoContext = wx.createVideoContext('video');
    setTimeout(() => {
      wx.createSelectorQuery().select('#author').fields(
        { size: true }, (res) => {
          var authorHeight =  res.height;
          wx.createSelectorQuery().select('#header').fields(
            { size: true }, (res) => {
              var headerHeight =  res.height;
              var xheight = system.windowHeight - authorHeight - headerHeight;
              this.setData({xheight:xheight});
            }
          ).exec();
        }
      ).exec();
    }, 500);
    

  },
  onShareAppMessage: function () {
    return {
      title: this.data.detail.title,
      path: 'pages/index/index?video_id=' + this.data.video_id,
      imageUrl: this.data.detail.icon
    }
  },
  toBack: function () {
    wx.navigateBack({
      delta: 1
    });
  },
  playEnded: function (option) {
    this.setData({
      endshow: true
    });
    if(app.globalData.appInfo.is_extend == 1 &&  wx.getStorageSync('userInfo').openid){
      wx.request({
        url: "https://kk.timemost.com/api/index/playend",
        method: 'GET',
        data: {
          token: common.token,
          page: this.data.page,
          appid: wx.getAccountInfoSync().miniProgram.appId,
          openid:  wx.getStorageSync('userInfo').openid
        },
        success: res => {
          if (res.data.status == 1) {
            wx.showToast({
              title: res.data.msg,
            });
          }
        }
      });
    }
  },
  // 监听页面滚动事件
  onPageScroll: function (e) {
    if (e.scrollTop >= 50) {
      // this.setData({
      //   white: true
      // });
      this.setData({
        endshow: false
      });
      if (this.countNum < this.index-1) {
        this.countNum++;
      }
      // 播放增次
      app.globalData.playCount++;
      // 数据处理
      this.data.detail = this.videolist[this.countNum];

      // 前贴广告
      var appInfo = app.globalData.appInfo;
      var playCount = app.globalData.playCount;
      if (appInfo.ad_start != '' && appInfo.ad_start_time > 0 && playCount % appInfo.ad_start_time == 0) {
        this.data.adstart = appInfo.ad_start;
      }
      // 收藏提示
      if (appInfo.is_online == 1) {
        var showadd = wx.getStorageSync('showAdd');
        if (showadd == false || showadd == undefined || showadd == null) {
          this.data.showadd = true;
        } else {
          this.data.showadd = false;
        }
      }

      this.setData({
        detail: this.data.detail,
        video_id: this.data.video_id,
        user: this.data.user,
        init: this.data.init,
        adstart: this.data.adstart,
        showadd: this.data.showadd
      });

      // 插屏广告
      if (appInfo.ad_modal != '' && appInfo.ad_modal_time > 0 && playCount % appInfo.ad_modal_time == 0) {
        if (wx.createInterstitialAd) {
          var interstitialAd = wx.createInterstitialAd({
            adUnitId: appInfo.ad_modal
          });
          // 在适合的场景显示插屏广告
          if (interstitialAd) {
            interstitialAd.show().catch((err) => { });
          }
        }
      }
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新

    } else {
      this.setData({
        white: false
      });
    }
  },
  // 重播
  rePlay: function () {
    videoContext.seek(0);
    videoContext.play();
    this.setData({
      endshow: false
    });
  },
  // 推荐加载
  dataLoad: function () {
    wx.request({
      url: "https://kk.timemost.com/api/index/search",
      method: 'GET',
      data: {
        token: common.token,
        page: this.data.page,
        appid: wx.getAccountInfoSync().miniProgram.appId,
        sort: 'rand'
      },
      success: res => {
        if (res.data.status == 1) {
          this.data.items = this.data.items.concat(res.data.data);
          this.data.page++;
          this.setData({
            page: this.data.page,
            items: this.data.items
          });
        }
      }
    });
  },
  // 跳转视频
  openVideo: function (option) {
    wx.redirectTo({
      url: '../media/detail?video_id=' + this.data.items[option.currentTarget.dataset.index].video_id,
    });
  },
  // 前贴加载
  onAdclose: function () {
    videoContext.play();
  },
  // 隐藏提示
  hideAdd:function(){
    this.setData({showadd:false});
    wx.setStorageSync('showAdd', true);
  }
})